<template lang="pug">
  keep-alive
    kalix-table(bizKey="createexam" title="考试列表"
    v-bind:targetURL="targetURL" v-bind:tableFields="tableFields" bizSearch="ExamSearch"
    v-bind:btnList="btnList" v-bind:bizDialog="bizDialog" v-bind:customTableTool="customTableTool"
    v-bind:toolbarBtnList="toolbarBtnList" v-bind:customToolBar="customToolBar"
    ref="examTable")
</template>

<script type="text/ecmascript-6">
  import {ExamCreateURL} from '../config.toml'
  import {ExamBtnList} from './config'

  export default {
    name: 'kalix-exam-createexam',
    data() {
      return {
        targetURL: ExamCreateURL,
        btnList: ExamBtnList,
        tableFields: [
          {prop: 'name', label: '考试名称'},
          {prop: 'subject', label: '考试科目'},
          {prop: 'examStart', label: '考试时间'},
          {prop: 'duration', label: '考试时长（分钟）'},
          {prop: 'examMinTime', label: '最少答题时长（分钟）'},
          {prop: 'paperName', label: '考卷模板'},
          {prop: 'paperChoice', label: '抽取考卷'},
          {prop: 'passScore', label: '及格线'},
          {prop: 'distributeStat', label: '考生分配状态'},
          {prop: 'teacherDistribute', label: '教师分配状态'}
        ],
        bizDialog: [
          {id: 'view', dialog: 'ExamView'},
          {id: 'edit', dialog: 'ExamEdit'},
          {id: 'add', dialog: 'ExamAdd'},
          {id: 'addExamUser', dialog: 'ExamUser'}
        ],
        toolbarBtnList: [
          {id: 'randomPaper', title: '随机卷抽取', isShow: true, icon: 'icon-add'}
        ]
      }
    },
    methods: {
      customTableTool(row, btnId, _kalixTable) {
        console.log('row', row)
        switch (btnId) {
          case 'addExamUser':
            _kalixTable.whichBizDialog = 'ExamUser'
            this.$nextTick(() => {
              _kalixTable.$refs.kalixDialog.$refs.kalixBizDialog.open('', false, row)
            })
            break
          case 'createExamPaper':
            let examId = row.id;
            this.preCreateExamPaper(examId)
            break
          case 'addExamTeacher':
            _kalixTable.whichBizDialog = 'ExamTeacher'
            this.$nextTick(() => {
              _kalixTable.$refs.kalixDialog.$refs.kalixBizDialog.open('', false, row)
            })
            break
        }
      },
      customToolBar(btnId, table) {
        switch (btnId) {
          case 'randomPaper': {
            this.onRandomPaper()
            break
          }
        }
      },
      onRandomPaper() {
        let arr = [1, 2, 3];
        let n1=Math.floor(Math.random()*3 + 1);
        let output = '随机试卷为：'
        for (let i=0; i < arr.length; i++) {
          if (arr[i] !== n1) {
            output += arr[i] + ' '
          }
        }
        this.$KalixMessage.success(output)
      },
      preCreateExamPaper(examId) {
        this.axios.request({
          method: 'POST',
          url: this.targetURL + "/" + examId + "/createexampaper",
          params: {
            // ids: ids.join(':')
            id: examId
          }
        }).then(response => {
          this.$KalixMessage.success(response.data.msg)
          this.$refs.examTable.refresh()
        })
      }
    }
  }
</script>

<style scoped lang="stylus" type="text/stylus">
</style>
